package model;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class BlogDao {

    // 插入一个 Blog 对象到数据库中
    public void insert(Blog blog) {
        // 由于与数据库建立连接已经在DBUtil类中创建了，因此我们可以直接使用
        // 在当前类中把连接置为空
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            //1.创建数据源
            connection = DBUtil.getConnection();
            //2.构造sql语句
            String sql = "insert into blog values(null,?,?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1,blog.getTitle());
            statement.setString(2,blog.getContent());
            statement.setInt(3,blog.getUserId());
            statement.setString(4,blog.getPostTime());
            //3.执行sql语句
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,null);
        }
    }

    //查询所有的博客
    public List<Blog> selectAll() {

        List<Blog> blogs = new ArrayList<>();
        //1.创建与数据库的连接
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            //2.构造sql语句
            String sql = "select * from blog order by postTime desc";
            //3.执行sql语句
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            //4.执行结果集
            while(resultSet.next()) {
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                // 此处正文内容如果超过100则超过100的部分用...表示
                String content = resultSet.getString("content");
                if(content.length() > 90) {
                    content = content.substring(0,90)+"...";
                }
                // 再把内容设置到blog中
                blog.setContent(content);
                blog.setUserId(resultSet.getInt("userId"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                // 将得到每一条结果依次存放到顺序表中
                blogs.add(blog);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return blogs;
    }

    // 通过blogId进行查询，返回是一个Blog对象
    public Blog selectOne(int blogId) {
        //创建连接，并把连接指向空
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            //1.与数据库建立连接
            connection = DBUtil.getConnection();
            //2.构造sql语句
            String sql = "select * from blog where blogId = ?";
            //3.将sql语句发送的数据库中，即执行sql语句
            statement = connection.prepareStatement(sql);
            // PreparedStatement中的setInd等，是用来替换sql语句中的占位符?的
            statement.setInt(1,blogId);
            // executeQuery的意为执行插入语句，即上述的SetInd等方法
            resultSet = statement.executeQuery();

            if(resultSet.next()) {
                Blog blog = new Blog();
                // getInt等方法用于从结果集的指定位置来获取数据
                blog.setBlogId(resultSet.getInt("userId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setUserId(resultSet.getInt("blogId"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                return blog;
            }

        }catch (SQLException e){
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        // 如果没有结果集，则返回空
        return null;
    }

    // 删除操作，通过blogId来删除
    public void delete(int blogId) {
        // 创建空的连接
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            //1.与数据库建立连接
            connection = DBUtil.getConnection();

            //2.构造sql语句
            String sql = "delete * from blog where blogId = ?";
            // 下条代码的意为预编译的sql语句准备好
            statement = connection.prepareStatement(sql);
            // 下条代码意为设置预编译的sql语句执行
            statement.setInt(1,blogId);

            //3.执行sql语句
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,null);
        }
    }
}
